// fpDoubleSinglePeer.cpp: implementation of the fpDoubleSinglePeer class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "fpDoubleSinglePeer.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#ifdef FPCONTROLS
#ifndef _UNICODE

IMPLEMENT_DYNAMIC(fpDoubleSinglePeer, CfpDouble)

/////////////////////////////////////////////////////////////////////////////
// fpDoubleSinglePeer

fpDoubleSinglePeer::fpDoubleSinglePeer(AWTComponent* comp)
{
	m_comp = comp;
	m_AWTWindow = NULL;		
}

fpDoubleSinglePeer::~fpDoubleSinglePeer()
{
	if (m_hWnd != NULL)
		DestroyWindow();
}


BEGIN_MESSAGE_MAP(fpDoubleSinglePeer, CfpDouble)
	//{{AFX_MSG_MAP(fpDoubleSinglePeer)
	ON_WM_KEYDOWN()
	ON_WM_CHAR()
	ON_WM_KILLFOCUS()
	ON_WM_SETFOCUS()
	ON_WM_KEYUP()
	ON_WM_CREATE()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// fpDoubleSinglePeer message handlers

void fpDoubleSinglePeer::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	time_t t;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTKeyEvent e(m_comp, AWTKeyEvent.KEY_PRESSED, t, nFlags, nChar, nChar);
	m_comp->processKeyEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTKEYEVENT, 0, (LPARAM)&e);

	CfpDouble::OnKeyDown(nChar, nRepCnt, nFlags);
}

void fpDoubleSinglePeer::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	time_t t;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTKeyEvent e(m_comp, AWTKeyEvent.KEY_TYPED, t, nFlags, nChar, nChar);
	m_comp->processKeyEvent(&e);
	if (nChar == VK_TAB)
		return;
	if (nChar == VK_RETURN)
		return;
	CfpDouble::OnChar(nChar, nRepCnt, nFlags);
}

void fpDoubleSinglePeer::OnKillFocus(CWnd* pNewWnd) 
{
	CfpDouble::OnKillFocus(pNewWnd);
	
	AWTFocusEvent e(m_comp, AWTFocusEvent.FOCUS_LOST);
	m_comp->processFocusEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTFOCUSEVENT, 0, (LPARAM)&e);
}

void fpDoubleSinglePeer::OnSetFocus(CWnd* pOldWnd) 
{
	CfpDouble::OnSetFocus(pOldWnd);

	AWTFocusEvent e(m_comp, AWTFocusEvent.FOCUS_GAINED);
	m_comp->processFocusEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTFOCUSEVENT, 0, (LPARAM)&e);
}

void fpDoubleSinglePeer::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	time_t t;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTKeyEvent e(m_comp, AWTKeyEvent.KEY_RELEASED, t, nFlags, 0, nChar);
	m_comp->processKeyEvent(&e);
	CfpDouble::OnKeyUp(nChar, nRepCnt, nFlags);
}

int fpDoubleSinglePeer::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CfpDouble::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	CWnd* wnd = this;
	CString str;
	BOOL bFound = FALSE;
	do {
		wnd = wnd->GetParent();
		wnd->GetWindowText(str);
		if (str == "!~AWTWindow~!")
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTDialogPeer)))
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTMdiChildWnd)))
		{
			bFound = TRUE;
			break;
		}
	} while (wnd != NULL);
	if (bFound)
		m_AWTWindow = wnd;		
	return 0;
}

void fpDoubleSinglePeer::OnDestroy() 
{
	CWnd::OnDestroy();
	AWTWindowEvent e(m_comp, AWTWindowEvent.WINDOW_CLOSED);
//	m_comp->processWindowEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTWINDOWEVENT, 0, (LPARAM)&e);
}

#endif
#endif //FPCONTROLD